﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DTcms.Common;

namespace DTcms.Web.admin.company
{
    public partial class xiangce_edit : Web.UI.ManagePage
    {
        private string action = DTEnums.ActionEnum.Add.ToString(); //操作类型
        private int id = 0;

        protected void Page_Load(object sender, EventArgs e)
        {
            string _action = DTRequest.GetQueryString("action");
            this.id = DTRequest.GetQueryInt("id");

            if (!string.IsNullOrEmpty(_action) && _action == DTEnums.ActionEnum.Edit.ToString())
            {
                this.action = DTEnums.ActionEnum.Edit.ToString();//修改类型
                if (this.id == 0)
                {
                    JscriptMsg("传输参数不正确！", "back");
                    return;
                }
                if (!new BLL.xiangce().Exists(this.id))
                {
                    JscriptMsg("内容不存在或已被删除！", "back");
                    return;
                }
            }
            if (!Page.IsPostBack)
            {
                if (action == DTEnums.ActionEnum.Edit.ToString()) //修改
                {
                    ShowInfo(this.id);
                }
            }
        }

        #region 赋值操作=================================
        private void ShowInfo(int _id)
        {
            BLL.xiangce bll = new BLL.xiangce();
            Model.xiangce model = bll.GetModel(_id);

            txtTitle.Text = model.title;
            txtImgUrl.Text = model.img_url;
            txtContent.Text = model.content;

            txtCompanyID.Text = model.company_id.ToString();
            if (model.company_id > 0)
            {
                lbCompanyName.Text = new BLL.company().GetTitle(model.company_id);
            }

            rptAlbumList.DataSource = model.albums;
            rptAlbumList.DataBind();
        }
        #endregion

        #region 增加操作=================================
        private bool DoAdd()
        {
            try
            {
                Model.xiangce model = new Model.xiangce();
                BLL.xiangce bll = new BLL.xiangce();

                model.title = txtTitle.Text;
                model.img_url = txtImgUrl.Text;
                model.content = txtContent.Text;
                model.add_time = DateTime.Now;
                model.company_id = Utils.StrToInt(txtCompanyID.Text, 0);

                #region 保存相册====================
                //检查是否有自定义图片
                if (txtImgUrl.Text.Trim() == "")
                {
                    model.img_url = hidFocusPhoto.Value;
                }
                string[] albumArr = Request.Form.GetValues("hid_photo_name");
                string[] remarkArr = Request.Form.GetValues("hid_photo_remark");
                if (albumArr != null && albumArr.Length > 0)
                {
                    List<Model.common_albums> ls = new List<Model.common_albums>();
                    for (int i = 0; i < albumArr.Length; i++)
                    {
                        string[] imgArr = albumArr[i].Split('|');
                        if (imgArr.Length == 3)
                        {
                            if (!string.IsNullOrEmpty(remarkArr[i]))
                            {
                                ls.Add(new Model.common_albums { channel_id = 6, original_path = imgArr[1], thumb_path = imgArr[2], remark = remarkArr[i] });
                            }
                            else
                            {
                                ls.Add(new Model.common_albums { channel_id = 6, original_path = imgArr[1], thumb_path = imgArr[2] });
                            }
                        }
                    }
                    model.albums = ls;
                }
                #endregion


                if (bll.Add(model) > 0)
                {
                    return true;
                }
            }
            catch
            {
                return false;
            }
            return false;
        }
        #endregion

        #region 修改操作=================================
        private bool DoEdit(int _id)
        {
            try
            {
                BLL.xiangce bll = new BLL.xiangce();
                Model.xiangce model = bll.GetModel(_id);

                model.title = txtTitle.Text;
                model.img_url = txtImgUrl.Text;
                model.content = txtContent.Text;
                model.add_time = DateTime.Now;
                model.company_id = Utils.StrToInt(txtCompanyID.Text, 0);

                #region 保存相册====================
                if (model.albums != null)
                {
                    model.albums.Clear();
                }
                string[] albumArr = Request.Form.GetValues("hid_photo_name");
                string[] remarkArr = Request.Form.GetValues("hid_photo_remark");
                if (albumArr != null)
                {
                    List<Model.common_albums> ls = new List<Model.common_albums>();
                    for (int i = 0; i < albumArr.Length; i++)
                    {
                        string[] imgArr = albumArr[i].Split('|');
                        int img_id = Utils.StrToInt(imgArr[0], 0);
                        if (imgArr.Length == 3)
                        {
                            if (!string.IsNullOrEmpty(remarkArr[i]))
                            {
                                ls.Add(new Model.common_albums { id = img_id, channel_id = 6, article_id = _id, original_path = imgArr[1], thumb_path = imgArr[2], remark = remarkArr[i] });
                            }
                            else
                            {
                                ls.Add(new Model.common_albums { id = img_id, channel_id = 6, article_id = _id, original_path = imgArr[1], thumb_path = imgArr[2] });
                            }
                        }
                    }
                    model.albums = ls;
                }
                #endregion

                if (bll.Update(model))
                {
                    return true;
                }
            }
            catch
            {
                return false;
            }
            return false;
        }
        #endregion

        //保存类别
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if (action == DTEnums.ActionEnum.Edit.ToString()) //修改
            {
                if (!DoEdit(this.id))
                {

                    JscriptMsg("保存过程中发生错误！", "");
                    return;
                }
                JscriptMsg("修改类别成功！", "xiangce_list.aspx");
            }
            else //添加
            {
                if (!DoAdd())
                {
                    JscriptMsg("保存过程中发生错误！", "");
                    return;
                }
                JscriptMsg("添加类别成功！", "xiangce_list.aspx");
            }
        }
    }
}